/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.model;

import coalescent.data.PopGenData;

import com.google.common.collect.ImmutableSet;

import commons.core.Model;
import commons.core.Singletons;
import commons.util.ExceptionUtil;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.EnumSet;

/**
 * A population genetic model is characterized by a set of event types.
 *
 * @param <E> enum representing event types for this model
 * @author Susanta Tewari
 * @since 1.3.0
 */
public abstract class PopGenModel<E extends Enum<E>> implements Model {

    /** Field description */
    private ImmutableSet<E> eventTypes;

    /**
     * Initializes the event types.
     *
     * @param eventType enum class representing event types for this model
     * @throws NullPointerException if, parameter is {@code null}
     * @throws IllegalArgumentException if, {@code eventType} is empty
     */
    public PopGenModel(Class<E> eventType) {

        this.eventTypes = ImmutableSet.copyOf(EnumSet.allOf(eventType));

        if (eventTypes.isEmpty()) {

            ExceptionUtil.throwIllegalArgEx(getClass(), "PopGenModel_Event_Enum_Empty",
                                            eventType.getName());
        }
    }

    /**
     * Each event type represents the type of a population genetic event under this model.
     *
     * @return all the event types of this model
     */
    public final ImmutableSet<E> getEventTypes() {
        return eventTypes;
    }

    /**
     * Equals based on the event types.
     *
     * @param obj object to compare against
     *
     * @return {@code true} if the event types are same; {@code false} otherwise
     */
    @Override
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        //J-
        @SuppressWarnings("unchecked")
        //J+
        final PopGenModel<E> other = (PopGenModel<E>) obj;

        return new EqualsBuilder().append(eventTypes, other.eventTypes).isEquals();
    }

    /**
     * Computes hash code based on the event types.
     *
     * @return hash code based on the event types
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(eventTypes).toHashCode();
    }

    /**
     * Contains information on event types.
     *
     * @return information on event types.
     */
    @Override
    public String toString() {

        return new ToStringBuilder(this, Singletons.TO_STRING_STYLE).append("event-types",
                                   eventTypes).toString();
    }
}
